package com.listener;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import common.utils.MailUtil;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;

public class MyListener implements MessageListener {

    private static final ObjectMapper MAPPER = new ObjectMapper();
    public void onMessage(Message message) {
        /*byte[] msg = message.getBody();
        Map map = (Map) SerializationUtils.deserialize(msg);
        String to = (String) map.get("to");
        String subject = (String) map.get("subject");
        String content = (String) map.get("content");

        System.out.println(to);
        System.out.println(subject);
        System.out.println(content);*/

        try {
            //msg就是rabbitmq传来的消息，需要的同学自己打印看一眼
            // 使用jackson解析
//            System.out.println(message.toString());
            JsonNode jsonData = MAPPER.readTree(message.getBody());
            System.out.println(jsonData.get("to").asText());
            System.out.println(jsonData.get("subject").asText());
            System.out.println(jsonData.get("content").asText());

            String to = jsonData.get("to").asText();
            String subject = jsonData.get("subject").asText();
            String content = jsonData.get("content").asText();

            MailUtil.sendMsg(to, subject, content);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
